<template>
  <!-- 生产报工表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="生产报工单编号" prop="produceReportCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.produceReportCode"
              placeholder="请输入生产报工单编号"
              clearable
            />

            <span v-else>{{ formData.produceReportCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="周转卡编号" prop="produceTransferFormCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.produceTransferFormCode"
              placeholder="请输入周转卡编号"
              clearable
            />

            <span v-else>{{ formData.produceTransferFormCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="派工单编号" prop="workingFormCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workingFormCode"
              placeholder="请输入派工单编号"
              clearable
            />

            <span v-else>{{ formData.workingFormCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心编号" prop="workCenterCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCenterCode"
              placeholder="请输入工作中心编号"
              clearable
            />

            <span v-else>{{ formData.workCenterCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="线边库名称" prop="produceStorageName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.produceStorageName"
              placeholder="请输入线边库名称"
              clearable
            />

            <span v-else>{{ formData.produceStorageName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="工序id;mes_produce_routing_process.id"
            prop="processId"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processId"
              placeholder="请输入工序id;mes_produce_routing_process.id"
              clearable
            />

            <span v-else>{{ formData.processId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料id" prop="materialId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialId"
              placeholder="请输入物料id"
              clearable
            />

            <span v-else>{{ formData.materialId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料编号" prop="materialCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialCode"
              placeholder="请输入物料编号"
              clearable
            />

            <span v-else>{{ formData.materialCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成品数量" prop="productQuantity">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.productQuantity"
              placeholder="请输入成品数量"
              clearable
            />

            <span v-else>{{ formData.productQuantity }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量单位" prop="quantityUnit">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.quantityUnit"
              placeholder="请输入数量单位"
              clearable
            />

            <span v-else>{{ formData.quantityUnit }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否合格" prop="isValid">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.isValid"
              placeholder="请输入是否合格"
              clearable
            />

            <span v-else>{{ formData.isValid }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班次名称" prop="shiftName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.shiftName"
              placeholder="请输入班次名称"
              clearable
            />

            <span v-else>{{ formData.shiftName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班组编号" prop="crewCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.crewCode"
              placeholder="请输入班组编号"
              clearable
            />

            <span v-else>{{ formData.crewCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产批次" prop="produceBatch">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.produceBatch"
              placeholder="请输入生产批次"
              clearable
            />

            <span v-else>{{ formData.produceBatch }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESPRODUCEREPORTRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  produceReportCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  produceTransferFormCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workingFormCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  produceStorageName: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productQuantity: [],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  isValid: [],
  shiftName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  crewCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  produceBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  produceReportCode: "",
  produceTransferFormId: "",
  produceTransferFormCode: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  routingId: "",
  workingFormId: "",
  workingFormCode: "",
  workSequence: "",
  workCenterId: "",
  workCenterCode: "",
  produceStorageId: "",
  produceStorageName: "",
  processId: "",
  materialId: "",
  materialCode: "",
  productQuantity: "",
  quantityUnit: "",
  isValid: "",
  startTime: "",
  completeTime: "",
  totalTime: "",
  totalCost: "",
  shiftId: "",
  shiftName: "",
  crewId: "",
  crewCode: "",
  groupsIron: "",
  matTransferDocument: "",
  produceBatch: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        produceReportCode,
        produceTransferFormId,
        produceTransferFormCode,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        routingId,
        workingFormId,
        workingFormCode,
        workSequence,
        workCenterId,
        workCenterCode,
        produceStorageId,
        produceStorageName,
        processId,
        materialId,
        materialCode,
        productQuantity,
        quantityUnit,
        isValid,
        startTime,
        completeTime,
        totalTime,
        totalCost,
        shiftId,
        shiftName,
        crewId,
        crewCode,
        groupsIron,
        matTransferDocument,
        produceBatch,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        produceReportCode,
        produceTransferFormId,
        produceTransferFormCode,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        routingId,
        workingFormId,
        workingFormCode,
        workSequence,
        workCenterId,
        workCenterCode,
        produceStorageId,
        produceStorageName,
        processId,
        materialId,
        materialCode,
        productQuantity,
        quantityUnit,
        isValid,
        startTime,
        completeTime,
        totalTime,
        totalCost,
        shiftId,
        shiftName,
        crewId,
        crewCode,
        groupsIron,
        matTransferDocument,
        produceBatch,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.produceReportCode = "";
  formData.produceTransferFormId = "";
  formData.produceTransferFormCode = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.routingId = "";
  formData.workingFormId = "";
  formData.workingFormCode = "";
  formData.workSequence = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.produceStorageId = "";
  formData.produceStorageName = "";
  formData.processId = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.productQuantity = "";
  formData.quantityUnit = "";
  formData.isValid = "";
  formData.startTime = "";
  formData.completeTime = "";
  formData.totalTime = "";
  formData.totalCost = "";
  formData.shiftId = "";
  formData.shiftName = "";
  formData.crewId = "";
  formData.crewCode = "";
  formData.groupsIron = "";
  formData.matTransferDocument = "";
  formData.produceBatch = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 生产报工保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesproducereportrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
